// pages/yhq/yhq.js
var api = require('../../utils/api.js');
var time = require("../../utils/time.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentTab: 0,
    list:[],
    size:10,
    loading:false
  },
  //滑动切换
  swiperTab: function (e) {
    let current = e.detail.current;
    let source = e.detail.source
    if (source === 'touch') {
      this.setData({
        currentTab: current,
        loading: false
      })
      this.couponList()
    }

   
  
  },
  //点击切换
  clickTab: function (e) { 
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current,
        loading: false
      })

      this.couponList()
    
    }
  },
  goShop(e){
    wx.navigateTo({
      url: '/pages/searchDiscounts/searchDiscounts?id=' + e.target.dataset.id + '&ticketDiscountName=' + e.target.dataset.name + '&ticketId=' + e.target.dataset.ticketid,
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.couponList()
  
  },


  //切换数据
  couponList:function(e){
   
    let that=this
    api.couponList({
       
      cityId: wx.getStorageSync("cityId"),
      status: that.data.currentTab, 
       page:{
         size:that.data.size
       }
    }).then(res => {
      if (res.result){
        res.object.records.forEach(function(v){
          v.endDate = time.formatTimeTwo(v.endDate, 'Y-M-D h:m')
        })
        that.setData({
          list: res.object.records,
          loading: true
        })
      }
     
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this
    let size=that.data.size
    size=size+10
    api.couponList({
      cityId: wx.getStorageSync("cityId"),
      status: that.data.currentTab,
      page: {
        size: taht.data.size
      }
    }).then(res => {
      if (res.result) {
        that.setData({
          list: res.object.records,
          size
        })
      }

    })
  },
})